package com.servlet;


import com.bean.*;
import com.dao.BhStaduntDao;
import com.dao.BhSyCaseDao;
import com.dao.BhtopicDao;
import com.dao.UserTopicDao;
import com.dao.impl.BhStudentDaoImpl;
import com.dao.impl.BhSyCaseDaoImpl;
import com.dao.impl.BhtopicDaoImpl;
import com.dao.impl.UserTopicDaoImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

/**
 * @author 冥尘
 * 题库操作servlet
 */
@WebServlet("/UserTopic/*")
public class UserTopicServlet extends BaseServlet {
    BhSyCaseDao bcd = new BhSyCaseDaoImpl();
    /**
     * 添加题库到数据库
     * @param request
     * @param response
     */
    public void insert(HttpServletRequest request, HttpServletResponse response){
        int uID=Integer.valueOf(request.getParameter("uID"));
        String tName=request.getParameter("tName");
        UserTopic ut=new UserTopic(uID,tName); //创建题库对象

        UserTopicDao utDao=new UserTopicDaoImpl();
        try {
            utDao.insertTopicKu(ut); //运行添加
            response.sendRedirect(""); //返回主页:记得添加路径
        } catch (SQLException | IOException throwables) {
            throwables.printStackTrace();
        }
    }

    /**
     * 跳转题库页
     * @param request
     * @param response
     */
    public void Teacher01(HttpServletRequest request, HttpServletResponse response){
        try {
            HttpSession session = request.getSession();
            BhUser bu = (BhUser) session.getAttribute("BhSyUser");
            List<UserTopicKu> btk = bcd.bhSelUserTK(bu.getuID());
            request.setAttribute("BhSyUserTK",btk);
            request.getRequestDispatcher("/WEB-INF/jsp/Teacher01.jsp").forward(request,response);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ServletException e) {
            e.printStackTrace();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

    public void selBhtopic(HttpServletRequest request, HttpServletResponse response){
        HttpSession session = request.getSession();

        String name=request.getParameter("name");
        int uid=Integer.valueOf(request.getParameter("uid"));
        System.out.println("name="+name+";uid="+uid);

        //设置题库名字为当前题库名
        session.setAttribute("TopicKuName", name);
        System.out.println("[已设置题库名TopicKuName]为"+name);

        BhtopicDao dao=new BhtopicDaoImpl();
        try {
            List<Bhtopic> list=dao.selectBhtopic(name,uid);
            request.setAttribute("bhtopic",list);
            request.getRequestDispatcher("/WEB-INF/jsp/Teacher01.jsp").forward(request,response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void Teacher03(HttpServletRequest request, HttpServletResponse response){
        System.out.println("1");
        try {
            BhStaduntDao dao = new BhStudentDaoImpl();
            request.setCharacterEncoding("utf-8");
            HttpSession session = request.getSession();
            BhUser bu = (BhUser) session.getAttribute("BhSyUser");

            List stu = dao.selStu(bu.getuClass());
            System.out.println(bu.getuClass());
            System.out.println(stu);

            request.setAttribute("stu",stu);
            request.getRequestDispatcher("/WEB-INF/jsp/Teacher03.jsp").forward(request,response);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ServletException e) {
            e.printStackTrace();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

    public void Teacher03_Page(HttpServletRequest request,HttpServletResponse response) throws SQLException, ServletException, IOException {
        String p= request.getParameter("p");
        int intp=1;
        if (p!=null) {
            intp=Integer.parseInt(p);
        }
        BhStaduntDao b = new BhStudentDaoImpl();

        int hs=b.hs();
        PageBean pb=new PageBean();
        pb.setTotal(hs);

        HttpSession session=request.getSession();
        session.setAttribute("curpage", intp);
        BhUser bu = (BhUser) session.getAttribute("BhSyUser");
        String cl = bu.getuClass();

        pb.setCurpage(intp);
        List stud=b.getPage(cl,intp);
        request.setAttribute("pb", pb);
        request.setAttribute("stud",stud);
        request.getRequestDispatcher("/WEB-INF/jsp/Teacher03.jsp").forward(request,response);


    }

    public void Teacher03_del(HttpServletRequest request,HttpServletResponse response) throws SQLException, IOException {
        System.out.println("delect");
        request.setCharacterEncoding("utf-8");

        String uid=request.getParameter("uid");

        BhStaduntDao b = new BhStudentDaoImpl();

        try {
            int num = Integer.parseInt(uid);
            int r=b.delStu(num);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        HttpSession session=request.getSession();
        int s= (Integer)session.getAttribute("curpage");
        response.sendRedirect("../UserTopic/Teacher03_Page?p="+s);
    }

    public void Teacher03_up(HttpServletRequest request,HttpServletResponse response) throws SQLException, IOException {
        System.out.println("update");
        request.setCharacterEncoding("utf-8");
        BhStaduntDao b = new BhStudentDaoImpl();

        String upuClass=request.getParameter("upuClass");
        String upuID=request.getParameter("upuID");
        String upuName=request.getParameter("upuName");
        String upuImg=request.getParameter("upuImg");
        String upuSystem=request.getParameter("upuSystem");

        int uid = Integer.parseInt(upuID);
        int usystem = Integer.parseInt(upuSystem);
        System.out.println("aaaaa:"+upuName);
        BhUser b1=new BhUser(upuClass, uid,upuName,upuImg ,usystem);
//        System.out.println(b1);
        try {
            int r=b.updStu(b1);

        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        HttpSession session=request.getSession();
        int s= (Integer)session.getAttribute("curpage");
        response.sendRedirect("../UserTopic/Teacher03_Page?p="+s);

    }

    public void Teacher03_in(HttpServletRequest request,HttpServletResponse response) throws SQLException, IOException {
        System.out.println("insert");

        request.setCharacterEncoding("utf-8");
        BhStaduntDao b = new BhStudentDaoImpl();

        String upuClass=request.getParameter("upuClass");
        String upuID=request.getParameter("upuID");
        String upuName=request.getParameter("upuName");
        String upuImg=request.getParameter("upuImg");
        String upuSystem=request.getParameter("upuSystem");

//        int uid = Integer.parseInt(upuID);
//        int usystem = Integer.parseInt(upuSystem);
//        BhUser b1=new BhUser(upuClass, uid,upuName ,usystem);
        BhUser b1=new BhUser(upuClass,upuName );
        try {
            int r=b.inStu(b1);

        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        HttpSession session=request.getSession();
        int s= (Integer)session.getAttribute("curpage");
        response.sendRedirect("../UserTopic/Teacher03_Page?p="+s);

    }

}
